//
//  LSChooseView.swift
//  新浪微博---01次
//
//  Created by 李宗帅 on 16/5/12.
//  Copyright © 2016年 LS. All rights reserved.
//

import UIKit

let LSChooseViewWithReuseIdentifier = "LSChooseViewWithReuseIdentifier"

let LSChooseViewMargin: CGFloat = 5

//给cell定义一个代理  当点击删除按钮的时候 让代理者执行方法
protocol LSChooseViewCellDelegate:NSObjectProtocol {
    
    func didChooseViewCellDelegateButtonClick(index:Int)
    
}
class LSChooseView: UICollectionView ,UICollectionViewDataSource,LSChooseViewCellDelegate,UICollectionViewDelegate{
    
    //定义一个闭包,当点击加号的cell的时候,回调给控制器执行
    var closure:(()->())?

    private var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    
    private var images :[UIImage] = [UIImage]()
    
    //暴漏的接口,用这个方法 可以往数据源数组里边添加图片
    func addImage(image: UIImage){
        
        //在数据源数组里边添加图片
        images.append(image)
        
        //刷新数据
        reloadData()
    
    }
    
    //对外公开一个方法,返回图片数组
    func getImages() -> [UIImage]{
        return images
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor.blueColor()
        
        dataSource = self
        
        delegate = self
        
        //注册cell
        registerClass(LSChooseViewCell.self, forCellWithReuseIdentifier: LSChooseViewWithReuseIdentifier)
        
        
        let itemWH = (kUIScreenW - 2 * 10 - 2 * LSChooseViewMargin) / 3
        
        //设置itemSize大小
        flowLayout.itemSize = CGSizeMake(itemWH, itemWH)
        
        
        //设置itemSize的间隔
        
        
        flowLayout.minimumInteritemSpacing = LSChooseViewMargin
        flowLayout.minimumLineSpacing = LSChooseViewMargin
        
        
    }

}


    //数据源方法
extension LSChooseView {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if images.count == 0 {return 0}
        if images.count == 9 {return 9}
        
        return images.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LSChooseViewWithReuseIdentifier, forIndexPath: indexPath) as! LSChooseViewCell
        
        
        //当要显示的内容和数组长度一致的时候,代表数组里的内容已经完毕
        if images.count == indexPath.row {
            //那么让当前的图片显示为nli
            cell.image = nil
            
        } else {
        
            cell.image = images[indexPath.row]
        }
        
        
        cell.indexPath = indexPath
        
        cell.delegate = self
        
        
        return cell
    }
    
    //实现cell的代理方法
    func didChooseViewCellDelegateButtonClick(index: Int) {
        
        //删除图片
        images.removeAtIndex(index)
        
        //刷新数据
        
        reloadData()
    }
    
    //点击cell的代理方法
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == images.count {
            
            //让控制器执行闭包  回调方法
            closure?()
        }
    }
}


class LSChooseViewCell: UICollectionViewCell {
    
    //定义一个图片的属性  给背景图片赋值
    var image:UIImage? = UIImage(){
        didSet {
            if image == nil {
                
                iconView.image = UIImage(named: "compose_pic_add")
                
                //让按钮隐藏
                deleteBtn.hidden = true
            } else {
                
                iconView.image = image
                
                //让删除按钮显示
                deleteBtn.hidden = false
            }
            
        }
    }
    
    
    //定义一个indexPath属性, 方便点击删除按钮的时候,能够回调,知道删除那个
    var indexPath:NSIndexPath?
    
    //添加代理属性
    weak var delegate : LSChooseViewCellDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //添加视图和按钮到cell  并设置约束
        addSubview(iconView)
        addSubview(deleteBtn)
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView)
        }
        
        deleteBtn.snp_makeConstraints { (make) -> Void in
            make.top.right.equalTo(contentView)
        }
        
        
    }
    
    @objc private func clickDeleteBtn(){
        
        
        //当点击按钮的时候让代理者实现该方法
        delegate?.didChooseViewCellDelegateButtonClick(indexPath!.item)
    }
    
    
    //懒加载 cell的背景图片
    private lazy var iconView: UIImageView = UIImageView()
    
    //懒加载 右上删除按钮
    private lazy var deleteBtn:UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        
        //大小
        btn.sizeToFit()
        
        //添加点击事件
        btn.addTarget(self, action: "clickDeleteBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    
    }()
    
}